﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hi_Baidu.Packet;
using Hi_Baidu.Timer;
using System.Threading;
using Hi_Baidu.ProcessModel;

namespace Hi_Baidu.Notify.NotifyObject
{
    /// <summary>
    ///     提供了申请好友的提示
    /// </summary>
    class Notify_AddFriend : Notify
    {
        public Notify_AddFriend(String Key, BaseInPackage inpackage, NotifyType.NOTIFYTYPE NotifyType)
            : base(Key, inpackage, NotifyType, null)
        {
        }
        public override void Trigger()
        {
            //如果当前不存在该好友申请窗口，则提示
            if (!Globle.popWindowManager.Equals("AddFriendWndow:" + (this.NotifyPackage as BaseInPackage).GetUsername()))
            {
                Win_AddFriend win_AddFriend = new Win_AddFriend("AddFriendWndow:" + (this.NotifyPackage as BaseInPackage).GetUsername(), this);
                Globle.popWindowManager.Add(win_AddFriend);
                win_AddFriend.Show();
            }
            //存在则激活
            else
            {
                Globle.popWindowManager.ActiveWindow("AddFriendWndow:" + (this.NotifyPackage as BaseInPackage).GetUsername());
            }
        }

        /// <summary>
        ///     得到申请留言
        /// </summary>
        /// <returns>返回申请留言</returns>
        public String GetMessage()
        {
            return Encoding.Default.GetString((this.NotifyPackage as BaseInPackage).Body["MESSAGE"]).Replace("*@^@*", "");
        }

        /// <summary>
        ///     同意好友申请
        /// </summary>
        public void Agreen(String ItemId)
        {
            FriendProcessModel.PostAddFriendResult
                (
                    Hi_Baidu.ProcessModel.FriendProcessModel.ADDFRIEND_RESULT.SUCESSFUL,
                    this,
                    Globle.user.DIC_USERINFO["USERNAME"],
                    BitConverter.GetBytes(int.Parse(ItemId))
                );
            AddTimeOutTimer(Hi_Baidu.ProcessModel.FriendProcessModel.ADDFRIEND_RESULT.SUCESSFUL, Encoding.Default.GetBytes(this.GetUsername()), BitConverter.GetBytes(int.Parse(ItemId)));
        }

        /// <summary>
        ///     拒绝好友申请
        /// </summary>
        public void Refuse()
        {
            FriendProcessModel.PostAddFriendResult
                (
                    Hi_Baidu.ProcessModel.FriendProcessModel.ADDFRIEND_RESULT.FAIL,
                    this,
                    Globle.user.DIC_USERINFO["USERNAME"],
                    BitConverter.GetBytes(0)
                );
            //AddTimeOutTimer(Hi_Baidu.ProcessModel.FriendProcessModel.ADDFRIEND_RESULT.FAIL, this.GetUsername(), null);
        }

        /// <summary>
        ///     加入超时器
        /// </summary>
        private void AddTimeOutTimer(Hi_Baidu.ProcessModel.FriendProcessModel.ADDFRIEND_RESULT Result, byte[] username, byte[] ItemId)
        {
            //创建超时器
            PostAddFriendResult_TimeOutEvent postAddFriendResult_TimeOutEvent = new PostAddFriendResult_TimeOutEvent
                (
                    Result, 
                    username,
                    this,
                    ItemId,
                    "PostAddFriendResult:" + this.GetUsername(),
                    10000, 
                    3
                );
            //打入超时器
            Globle.timeoutTimerManager.InstallTimer(postAddFriendResult_TimeOutEvent);
        }
    }
}
